% -- A Ramsey Theory of Financial Distortions --
% Steady state and transition code for the infinite-horizon model
% Plotting Figure 3 and Figure 4


clc; clear;  close all;  format short


%% initilization
transition_     = 1;    % if transition_ is 1, then run the transition dynamics
plotting_       = 1;    % if plotting_ is 1, then plot the transition dynamics
fiscal_shock    = 1;    % if fiscal_shock is 1, fiscal-shock transition dynamics are computed
gov_spending_up = 0.1;  % the size of government spending shock

T               = 300;  % periods of time when the economy reaches steady state
shock_start     = 10;   % the period when the shock hits
shock_duration  = 10;   % periods of the shock duration
time_last       = 20;   % periods that we want to show on the graph


%% Compute the steady state (to be stored and used later) again with calibrated parameters

load param

par = par_calibrated; % Note: par_calibrated is stored in param

display('1. ########### I am solving the steady state ########################')

%%%% Pass structure 'par' to AMPL data file
cd steady_state_transition_AMPL
data_file = 'consumption_saving.dat'; % to be used in writing_data_to_AMPL.m
data_     = par; % to be used in writing_data_to_AMPL.m
writing_data_to_AMPL

system('ampl ss_unconstrained.run');

%%%% Pass AMPL output to structure 'par'
output_file = 'eqm_result.csv';
writing_data_from_AMPL % the output is parameters
for i = 1:length(parameters)
   par.(parameters{i,1}) = parameters{i,2};
end

if isfield(par, 'G_Y')  == 1
    par = rmfield(par, {'G_Y', 'I_Y', 'L_target'}); % remove par.G_Y etc as these are targets used for calibration
end
display('Done!')
display(' ')


%% Add initial values and pass the values back to AMPL again for transition analysis

%%%% Solve the transition dynamics
if transition_ == 1  
    K_ini               = par.K_SS;
    B_ini               = par.B_SS;
    tau_k_0             = 0; % initial tax rate
    
    par.K_ini           = K_ini;
    par.B_ini           = B_ini;
    par.tau_k_0         = tau_k_0;
  
    par.T               = T;
    par.shock_start     = shock_start;
    par.shock_duration  = shock_duration;
    par.fiscal_shock    = fiscal_shock;
    par.gov_spending_up = gov_spending_up;

    data_file   = 'transition_data.dat';
    data_       = par;
    writing_data_to_AMPL        
    
    %%%% Below we run transition dynamics in our paper and transition
    %%%% dynnamics in a traditional Ramsey model; We calculate transition
    %%%% dynamics in the two models with and without government spending
    %%%% shocks

    
    display('2. ########### Transition path: Ramsey outcomes ########################')
    %%%% Run the ramsey problem in our paper
    system('ampl transition_ramsey.run');
    display('Done!')
    display(' ')
    
    display('3. ########### Transition path: Traditional Ramsey outcomes ########################')        
    %%%% Run the traditional ramsey problem (no financing constraints)
    system('ampl transition_ramsey_traditional.run');  
    display('Done!')
    display(' ')

    display('4. ########### Save Results and Plotting ########################')            
    %%%% Pass AMPL output to structure 'transition' and 'transition_traditional' 
    output_file = 'transition.csv';
    writing_data_from_AMPL % the output is the set of parameters
    for i = 1:length(parameters)
        transition.(parameters{i,1}) = parameters{i,2};
    end   
    output_file = 'transition_traditional.csv';
    writing_data_from_AMPL %output is parameters
    for i = 1:length(parameters)
        transition_traditional.(parameters{i,1}) = parameters{i,2};
    end

    %%%% Pass AMPL output to structure 'transition_fiscal_shocks' and 'transition_traditional_fiscal_shocks'
    output_file = 'transition_fiscal_shocks.csv';
    writing_data_from_AMPL %output is the set of parameters
    for i = 1:length(parameters)
        transition_fiscal_shocks.(parameters{i,1}) = parameters{i,2};
    end        
    output_file = 'transition_traditional_fiscal_shocks.csv';
    writing_data_from_AMPL %output is parameters
    for i = 1:length(parameters)
        transition_traditional_fiscal_shocks.(parameters{i,1}) = parameters{i,2};
    end

    delete *.csv
    cd ..
    
    %%%% Now, we are ready to plot the transition dynamics
    if plotting_ == 1
        save (['transition_baseline_eta_positive'], 'transition')
        save (['transition_fiscal_shocks_eta_positive'], 'transition_fiscal_shocks')
        save (['transition_baseline_eta0'], 'transition_traditional')
        save (['transition_fiscal_shocks_eta0'], 'transition_traditional_fiscal_shocks')
        par.shock_start = shock_start;

        plot_comparison_wei % a separate .m file to plot Figure 3 and Figure 4
    end
    
end

